package com.feng.online_xdclass.exception;

import com.feng.online_xdclass.utils.JsonDate;
import com.mysql.cj.xdevapi.XDevAPIError;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import sun.awt.SunHints;

import java.util.logging.Logger;

/**
 * 异常处理类
 */
@ControllerAdvice
public class CustomExceptionHandler {

    private final static org.slf4j.Logger logger= LoggerFactory.getLogger(CustomExceptionHandler.class);
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public JsonDate handler(Exception e){
        logger.error("[系统异常]{}",e.getMessage());
      if(e instanceof XDexception){
          XDexception xDexception=(XDexception) e;
          return JsonDate.buildError(xDexception.getCode(),xDexception.getMsg());
      }else{
          return JsonDate.buildError("全局异常,未知错误");
      }
    }
}
